package org.dromara.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.servlet.http.HttpServletRequest;
import org.dromara.app.appParams.AppPageParams;
import org.dromara.app.appReturn.AppPriceLogRetuen;
import org.dromara.app.cssUtils.CssUtils;
import org.dromara.app.domain.SkAppUser;
import org.dromara.app.mapper.SkAppUserMapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.app.domain.bo.SkPriceLogBo;
import org.dromara.app.domain.vo.SkPriceLogVo;
import org.dromara.app.domain.SkPriceLog;
import org.dromara.app.mapper.SkPriceLogMapper;
import org.dromara.app.service.ISkPriceLogService;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户金额日志Service业务层处理
 *
 * @author Evacuation
 * @date 2024-07-23
 */
@RequiredArgsConstructor
@Service
public class SkPriceLogServiceImpl implements ISkPriceLogService {

    private final SkPriceLogMapper baseMapper;

    private final SkAppUserMapper skAppUserMapper;

    /**
     * 查询用户金额日志
     *
     * @param id 主键
     * @return 用户金额日志
     */
    @Override
    public SkPriceLogVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询用户金额日志列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 用户金额日志分页列表
     */
    @Override
    public TableDataInfo<SkPriceLogVo> queryPageList(SkPriceLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SkPriceLog> lqw = buildQueryWrapper(bo);
        Page<SkPriceLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的用户金额日志列表
     *
     * @param bo 查询条件
     * @return 用户金额日志列表
     */
    @Override
    public List<SkPriceLogVo> queryList(SkPriceLogBo bo) {
        LambdaQueryWrapper<SkPriceLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SkPriceLog> buildQueryWrapper(SkPriceLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SkPriceLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), SkPriceLog::getTitle, bo.getTitle());
        lqw.eq(bo.getType() != null, SkPriceLog::getType, bo.getType());
        lqw.eq(bo.getPrice() != null, SkPriceLog::getPrice, bo.getPrice());
        lqw.eq(bo.getUserId() != null, SkPriceLog::getUserId, bo.getUserId());
        lqw.eq(bo.getOldPrice() != null, SkPriceLog::getOldPrice, bo.getOldPrice());
        lqw.orderByDesc(SkPriceLog::getId);
        return lqw;
    }

    /**
     * 新增用户金额日志
     *
     * @param bo 用户金额日志
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SkPriceLogBo bo) {
        SkPriceLog add = MapstructUtils.convert(bo, SkPriceLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户金额日志
     *
     * @param bo 用户金额日志
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SkPriceLogBo bo) {
        SkPriceLog update = MapstructUtils.convert(bo, SkPriceLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SkPriceLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除用户金额日志信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
    /**
     * 钱包记录
     * @param params
     * @param req
     * @return
     */
    @Override
    public List<AppPriceLogRetuen> getPriceLog(AppPageParams params, HttpServletRequest req) {
        Long pageNumber = (params.getPageNum()-1)* params.getPageSize();
        List<SkPriceLog> list = baseMapper.selectList(
            new QueryWrapper<SkPriceLog>().lambda()
                .eq(SkPriceLog::getUserId, CssUtils.getUserId(req))
                .orderByDesc(SkPriceLog::getCreateTime)
                .last("limit "+pageNumber+","+params.getPageSize())
        );
        List<AppPriceLogRetuen> listAll = BeanUtil.copyToList(list,AppPriceLogRetuen.class);
        return listAll;
    }

    @Override
    public void withdrawimmediatelyjujue(Long userId, BigDecimal price) {

    }

}
